class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
     TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
}
public class project {
    int count=0;
    int sum=0;
    int ret=0;
    public int averageOfSubtree(TreeNode root) {
        dfs(root);
        return ret;
    }
    public void dfs(TreeNode root){
        if(root==null){
            return;
        }
        this.sum=0;
        this.count=0;
        process(root);
        if((this.sum/this.count)==root.val){
            this.ret++;
        }
        dfs(root.left);
        dfs(root.right);
    }
    public void process(TreeNode root){
        if(root==null){
            return;
        }
        this.sum+=root.val;
        this.count++;
        process(root.left);
        process(root.right);
    }





















    public String largestGoodInteger(String num) {
        int len=num.length();
        String ret="";
        int max=Integer.MIN_VALUE;
        for(int i=0;i<len-2;i++){
            String str=num.substring(i,i+3);
            if(find(str)){
                int value=Integer.parseInt(str);
                if(value>max){
                    max=value;
                    ret=str;
                }
            }
        }
        return ret;
    }
    public boolean find(String str){
        for(int i=0;i<str.length()-1;i++){
            if(str.charAt(i)!=str.charAt(i+1)){
                return false;
            }
        }
        return true;
    }





















    public int[] diStringMatch(String s) {
        int len=s.length();
        int[] arr=new int[len+1];
        int left=0;
        int right=len;
        int index=0;
        while (index<len){
            char ch=s.charAt(index);
            if(ch=='I'){
                arr[index]=left;
                left++;
            }else{
                arr[index]=right;
                right--;
            }
            index++;
        }
        arr[len]=left;
        return arr;
    }
}
